//undetermined :1、首次授权，未确定是否可使用此权限，此时调用对应的API时系统会弹出提示框让用户确认  
//authorized:	之前同意了授权
//denied:	之前拒绝了权限
//denied_always:	之前拒绝了权限
//none:	无此权限
//error:出错
const QuickAppPermission = {
	names:{
		PUSH:'',
		LOCATION:'',
		RECORD:'',
		CAMERA:'',
		ALBUM:'',//scope.album
		CALLPHONE:'',
		CONTACT:'',
		CALENDAR:'',
		MEMO:''
	},
	check(permissionID) {
		let permissionIdentity = permissionID.toUpperCase();
		return new Promise((resolve, reject) => {
			let permissionName = this.names[permissionIdentity];
			if (!permissionName){
				return resolve('none');
			}
			uni.getSetting({
				success(res) {
					console.log(res.authSetting)
					if (res.authSetting[permissionName] == undefined) {
						//1、首次授权，未确定是否可使用此权限，此时调用对应的API时系统会弹出提示框让用户确认  
						return resolve('undetermined');
					} else if (res.authSetting[permissionName] == true) {
						//2、之前同意了授权
						return resolve('authorized');
						
					} else if (res.authSetting[permissionName] != undefined &&
						res.authSetting[permissionName] != true) {
						//3、之前拒绝了权限
						return reject('denied');
						
					} else{
						return resolve('none');
					}
				},
				fail(err) {
					return reject('error',err);
				},
			})
		});

	},
	request(permissionID) {
		let permissionIdentity = permissionID.toUpperCase();
		return new Promise((resolve, reject) => {
			let permissionName = this.names[permissionIdentity];
			if (!permissionName){
				return resolve('none');
			}
			uni.authorize({
				scope:permissionName,
				success(res) {
					return resolve('authorized');
				},
				fail(err) {
					return reject('denied');
				},
			})
		});
	},	
	setting(permissionID='') {
		uni.openSetting({
			success(res) {
				
			},
			fail(err) {
				
			}
		});
	},
	get(permissionID,title=''){
		if (!title){
			title = '您请求的权限无法，请手动开启相关权限！'
		}
		let that = this;
		that.request(permissionID).catch(res=>{
			uni.showModal({
				title: '提示',
				content: title,
				success(res) {
					if (res.confirm) {
						that.setting(permissionID);
					}
				}
			});
		});
		
	}
	
}
export default QuickAppPermission;